package com.controller;

import java.util.List;

import com.bean.SelectItem;
import com.common.base.BaseController;
import com.common.exception.AppException;
import com.common.sysinit.CacheSysCode;
import com.common.utils.IdUtil;
import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.model.Syscode;
import com.service.CodeService;

@Before(Tx.class)
public class CodeController extends BaseController {
	public void enterCodeManage(){
		List<SelectItem> list = CodeService.service.getDmbhList();
		getRequest().setAttribute("dmbh", list);
		render("codeManage.jsp");
	}
	public void queryCode(){
		String dmbh = getPara("dmbh");
		
		dmbh = dmbh+"%";
		List<Syscode> list = CodeService.service.getSysCode(dmbh);
		refreshDataGrid(list);
	}
	public void enterCodeAdd(){
		List<SelectItem> list = CodeService.service.getDmbhList();
		getRequest().setAttribute("dmbh", list);
		render("codeAdd.jsp");
	}
	public void codeAdd(){
		Syscode code = getModel(Syscode.class,"code");
		if(StrKit.isBlank(code.getDmbh())){
			throw new AppException("代码编号为空");
		}
		if(StrKit.isBlank(code.getCode())){
			throw new AppException("code为空");
		}
		if(StrKit.isBlank(code.getText())){
			throw new AppException("text为空");
		}
		String dmbh = code.getDmbh();
		List<Syscode> list = CodeService.service.getDmmcByDmbh(dmbh);
		if(list.size()!=1){
			throw new AppException("code 代码表 ,代码编号"+code.getDmbh()+"数据配置错误");
		}
		
		List<Syscode> temp = CodeService.service.getSyscodeBYDmbhAndCode(code.getDmbh(), code.getCode());
		
		if(temp.size()>0){
			throw new AppException("已存在code为" + code.getCode() +"的代码");
		}
		
		String dmmc = list.get(0).getDmmc();
		code.setDmmc(dmmc);
		
		code.setSyscodeid(IdUtil.getId());
		code.save();
		//更新缓存的code
		CacheSysCode.cacheSyscode();
		
		renderSuccess();
		
	}
	public void enterCodeEdit(){
		String syscodeid = getPara("syscodeid");
		if(syscodeid == null){
			throw new AppException("请选中一行后编辑");
		}
		List<SelectItem> list = CodeService.service.getDmbhList();
		getRequest().setAttribute("dmbh", list);
		
		
		Syscode code = Syscode.dao.findById(syscodeid);
		getRequest().setAttribute("code", code);
		render("codeEdit.jsp");
	}
	public void codeEdit(){
		Syscode code = getModel(Syscode.class,"code");
		String syscodeid = code.getSyscodeid();
		String dmbh = code.getDmbh();
		String codeValue = code.getCode();
		String text = code.getText();
				
		if(StrKit.isBlank(syscodeid)){
			throw new AppException("改code不存在");
		}
		
		if(StrKit.isBlank(dmbh)){
			throw new AppException("代码编号为空");
		}
		
		if(StrKit.isBlank(codeValue)){
			throw new AppException("code值为空");
		}
		
		if(StrKit.isBlank(text)){
			throw new AppException("text为空");
		}
		
		List<Syscode> list = CodeService.service.getDmmcByDmbh(dmbh);
		if(list.size()!=1){
			throw new AppException("code 代码表 ,代码编号"+code.getDmbh()+"数据配置错误");
		}
		String dmmc = list.get(0).getDmmc();
		code.setDmmc(dmmc);
		
		code.update();
		
		CacheSysCode.cacheSyscode();
		renderSuccess();
	}
	public void codeDelete(){
		String syscodeid = getPara("syscodeid");
		Syscode.dao.deleteById(syscodeid);
		CacheSysCode.cacheSyscode();
		renderSuccess();
	}
}
